<?php

namespace App\Console\Crons;

use App\Ad\Postback\PostbackService;
use App\Commons\CommonConstant;
use App\Models\Log\LogAdPostback;

// 针对roi达标后的判断
class AdPostbackPayCron extends CronBase
{
    public function run($stime)
    {
        $status_list = [
            LogAdPostback::STATUS_MONEY_ROI_OK, // 当前roi已达标，暂停回传
            LogAdPostback::STATUS_ROIOK_KEEP_DELAY, // roi达标后-延迟回传未到达时间
        ];

        $end_time = $stime + 86400;
        $convert_type = CommonConstant::EVENT_PAY;
        $query = LogAdPostback::whereRaw("create_time>={$stime} AND create_time<{$end_time} AND convert_type={$convert_type}");
        $query->chunk(10000, function ($list) use ($status_list) {
            foreach ($list as $log) {
                if (in_array($log->status, $status_list)) {
                    PostbackService::getInstance()->postback($log);
                }
            }
        });
    }
}
